package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect1;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues.Article;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;

/**
 * 获取学生通知类试题作答
 */
@CrossOrigin
@RestController
public class Workcorrect1GetStuQues2AnswerController {

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workcorrect1/stu/ques2/id/answer/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String studentId, //
            @RequestParam String quesId, //
            @RequestParam String homeworkTaskReportQuesId, //
            RespModel respModel) throws ParseException {

        // 获得原题
        HomeworkTaskQues homeworkTaskQues = homeworkTaskQuesRepository.findByTaskIdAndQuesId(taskId, quesId);

        // 获取学生的作答
        HomeworkTaskReportQues homeworkTaskReportQues = homeworkTaskReportQuesRepository
                .findById(homeworkTaskReportQuesId);

        // return
        respModel.quesContent = homeworkTaskQues.getQuesContent();
        respModel.quesArticle = homeworkTaskQues.getQuesArticle();
        respModel.questionType = homeworkTaskQues.getQuesType();

        respModel.quesId = homeworkTaskReportQues.getQuesId();
        respModel.homeworkTaskReportQuesId = homeworkTaskReportQues.getId();
        respModel.duration = homeworkTaskReportQues.getDuration();
        respModel.mark = homeworkTaskReportQues.getMark();
        respModel.order = homeworkTaskReportQues.getOrder();
        respModel.answerText = homeworkTaskReportQues.getAnswerText();
        respModel.answerImgs = homeworkTaskReportQues.getAnswerImgs();
        if ((respModel.answerText != null && respModel.answerText.length() > 0)
                || (respModel.answerImgs != null && respModel.answerImgs.size() > 0)) {
            respModel.isStuAnswer = true;
        }
        // 通知类作业是否需要学生在线提交true需要,false不需要
        respModel.ifNeedSumbit = homeworkTaskQues.getIfNeedSumbit();
        return respModel;
    }

    /** 响应 **/
    protected static class RespModel extends YxyApiRespModel {
        public String quesContent; // 通知类作业 老师布置作业编辑的内容
        public List<Article> quesArticle; // 图片
        public String questionType; // 试题类型

        public String quesId; // 试题Id
        public String homeworkTaskReportQuesId; // homeworkTaskReportQues Id
        public Integer duration; // 做题时长
        public Integer mark; // 评分
        public Integer order; // 试题序号
        public String answerText; // 主观题 题目的回答
        public List<String> answerImgs; // 主观题 上传的图片
        public boolean isStuAnswer = false; // 学生是否作答
        public boolean ifNeedSumbit = true; // 通知类作业是否需要学生在线提交true需要,false不需要
    }
}
